package com.freesun.shop.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.freesun.shop.entity.Area;
import com.freesun.shop.mapper.AreaMapper;

import com.freesun.shop.service.AreaService;
import org.apache.dubbo.config.annotation.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.util.Assert;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zf
 * @since 2019-11-13
 */
@Service
@CacheConfig(cacheNames = "com.freesun.shop.entity.Area")
public class AreaServiceImpl extends ServiceImpl<AreaMapper, Area> implements AreaService {

    private static Logger logger = LoggerFactory.getLogger(AreaServiceImpl.class);
    private final static String ALL_AREA_LIST_PREFIX="'area-list'";
    @Override
    @Cacheable(key = ALL_AREA_LIST_PREFIX)
    public List<Area> list() {
        logger.info("查询所有的区域列表");
        return super.list();
    }

    @Override
    @CacheEvict(key = ALL_AREA_LIST_PREFIX)
    public boolean removeById(Serializable id) {
        logger.info("删除一个区域信息");
        Area area = getById(id);
        if (null == area) {
            throw new RuntimeException("要删除的区域信息不存在");
        }
        Integer subNodeNum = this.baseMapper.selectCount(new LambdaQueryWrapper<Area>()
                .eq(Area::getParentId, id));
        if (subNodeNum != null && subNodeNum > 0) {
            throw new RuntimeException("该节点含有子节点,不能删除.");
        }
        return super.removeById(id);
    }

    /**
     * 新增区域
     * @param entity
     * @return
     */
    @Override

    @Caching(evict = {
            @CacheEvict(key = ALL_AREA_LIST_PREFIX),
            @CacheEvict(key="'sub:'",allEntries = true)
    })
    public boolean save(Area entity) {
        Assert.notNull(entity,"新增的区域不能为Null");
        Assert.notNull(entity.getAreaName(),"新增的区域名称不能为Null");
        logger.info("新增一个区域，名称为:{}",entity.getAreaName());
        if (entity.getParentId() == null || entity.getParentId() == 0L) {
            entity.setLevel(1);
            entity.setParentId(0L);
        }else {
            Area area = getById(entity.getParentId());
            if (null == area) {
                throw new RuntimeException("要新增的父节点不存在");
            }
            entity.setLevel(area.getLevel() + 1);
        }
        return super.save(entity);
    }

    @Override
    @Caching(evict = {
            @CacheEvict(key = ALL_AREA_LIST_PREFIX),
            @CacheEvict(key="'sub:'",allEntries = true)
    })
    public boolean updateById(Area entity) {
        Assert.notNull(entity,"要修改的区域信息不能为Null");
        Assert.notNull(entity.getAreaId(),"要修改的区域信息ID不能为Null");
        logger.info("修改数据的ID:{}",entity.getAreaId());
        Area area = getById(entity.getAreaId());
        if (null == area) {
            throw new RuntimeException("要修改的数据不存在。");
        }
        entity.setParentId(entity.getParentId() == null ? 0L : entity.getParentId());
        if (entity.getParentId().equals(area.getParentId()) ) {
            throw new RuntimeException("父ID不一致");
        }
        boolean isOk=false;
        if (!area.getAreaName().equals(entity.getAreaName())) {
            isOk = super.updateById(entity);
        }
        return isOk;
    }

    @Override
    @Cacheable(key="'sub:'+#pid")
    public List<Area> listAreaByPid(Long pid) {
        logger.info("查询父id为{}的子选项",pid);
        return this.baseMapper.selectList(new LambdaQueryWrapper<Area>()
                .eq(Area::getParentId, pid));
    }
}
